using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIShopPanel : MonoBehaviour
{
	public NumericalEffect txtChips;

	public Text txtCountDown;

	public GameObject confirmWnd;

	public Image confirmItemIcon;

	public Text txtNotEnoughTitle;

	public Text txtNotEnoughDesc;

	public Text m_Physical;

	public Text m_PhysicalCooldownText;

	public Image m_PhysicalBar;

	public GameObject commonPanel;

	public ScrollRect cuePanel;

	public GameObject cueItemGO;

	public Sprite[] cueSpriteList;

	public GameObject switchCueBtn;

	public GameObject buyItemBtn;

	private List<UICueItem> _cutItemList = new List<UICueItem>();

	private int _useCue;

	private bool _showCuePanel;

	public int useCue
	{
		get
		{
			if (PlayerPrefs.HasKey("USE_CUE"))
			{
				return PlayerPrefs.GetInt("USE_CUE");
			}
			return -1;
		}
		set
		{
			PlayerPrefs.SetInt("USE_CUE", value);
		}
	}

	private void Start()
	{
		txtChips.InitValue(ConstantData.chips);
		OnRefreshPhysical();
		ConstantData.onChangeChips = (Action)Delegate.Combine(ConstantData.onChangeChips, new Action(OnRefreshChips));
		ConstantData.onChangePhysical = (Action)Delegate.Combine(ConstantData.onChangePhysical, new Action(OnRefreshPhysical));
		AddCueItem();
	}

	private void OnRefreshChips()
	{
		txtChips.value = ConstantData.chips;
	}

	public void OnRefreshPhysical()
	{
		m_Physical.text = ConstantData.physical + "/" + 20;
		m_PhysicalBar.fillAmount = (float)ConstantData.physical / 20f;
	}

	public void OpenLucklyPanel()
	{
		LaunchUIController.instance.OpenLucklyPanel();
	}

	public void WatchVideo()
	{
		AdManagerOld.instance.curAd.HasVideo();
	}

	public void GetFreeChips()
	{
		LaunchUIController.instance.OpenLucklyPanel();
	}

	public void OpenConfirmWnd(ItemInfo itemInfo)
	{
		confirmWnd.SetActive(value: false);
		confirmWnd.SetActive(value: true);
		confirmItemIcon.sprite = LaunchUIController.instance.GetSpriteFromName(itemInfo.icon);
	}

	public void OpenNotEnoughWnd()
	{
		UIADViewWnd.Show(UIADViewWnd.ADViewType.NotEnough);
	}

	public void BackToMain()
	{
		confirmWnd.SetActive(value: false);
		AdManagerOld.instance.curAd.HideBanner();
	}

	public void Update()
	{
		if (ConstantData.physical < 20)
		{
			m_PhysicalCooldownText.gameObject.SetActive(value: true);
			m_PhysicalCooldownText.text = EternalNode.Instance.physicalRecoverTime;
		}
		else
		{
			m_PhysicalCooldownText.gameObject.SetActive(value: false);
			m_PhysicalCooldownText.text = EternalNode.Instance.physicalRecoverTimeFull;
		}
	}

	private void OnDestroy()
	{
		ConstantData.onChangeChips = (Action)Delegate.Remove(ConstantData.onChangeChips, new Action(OnRefreshChips));
		ConstantData.onChangePhysical = (Action)Delegate.Remove(ConstantData.onChangePhysical, new Action(OnRefreshPhysical));
	}

	private void AddCueItem()
	{
		if (!ContainsCueItem(7))
		{
			UICueItem item = CreateCueItem(7);
			_cutItemList.Add(item);
			if (!PlayerPrefs.HasKey("USE_CUE"))
			{
				useCue = 7;
			}
		}
		if (ActivityManager.instance.IsTakenPrize(5) && !ContainsCueItem(8))
		{
			UICueItem item2 = CreateCueItem(8);
			_cutItemList.Add(item2);
			if (!PlayerPrefs.HasKey("USE_CUE") || useCue == 7 || useCue == 9)
			{
				useCue = 8;
			}
		}
		if (PlayerPrefs.HasKey("StarRateKey") && !ContainsCueItem(9))
		{
			UICueItem item3 = CreateCueItem(9);
			_cutItemList.Add(item3);
		}
		List<ShopInfo> shopInfos = ShopInfoMgr.shopInfos;
		for (int i = 0; i < shopInfos.Count; i++)
		{
			ShopInfo shopInfo = shopInfos[i];
			if (shopInfo.itemInfo.type == 6 && !ContainsCueItem(shopInfo.itemId))
			{
				UICueItem uICueItem = SupportTools.AddChild<UICueItem>(cuePanel.content.gameObject, cueItemGO);
				uICueItem.parentPanel = this;
				uICueItem.SetData(shopInfo.itemId, shopInfo.itemInfo.name, shopInfo.price);
				uICueItem.imgIcon.sprite = GetCueSprite(shopInfo.itemInfo.icon);
				_cutItemList.Add(uICueItem);
			}
		}
		RefreshCueList();
	}

	private UICueItem CreateCueItem(int itemId)
	{
		ItemInfo info = ItemInfoMgr.GetInfo(itemId);
		UICueItem uICueItem = SupportTools.AddChild<UICueItem>(cuePanel.content.gameObject, cueItemGO);
		uICueItem.parentPanel = this;
		uICueItem.SetData(info.id, info.name, 0);
		uICueItem.imgIcon.sprite = GetCueSprite(info.icon);
		return uICueItem;
	}

	private bool ContainsCueItem(int itemId)
	{
		for (int i = 0; i < _cutItemList.Count; i++)
		{
			if (_cutItemList[i].itemId == itemId)
			{
				return true;
			}
		}
		return false;
	}

	public void RefreshCueList()
	{
		for (int i = 0; i < _cutItemList.Count; i++)
		{
			_cutItemList[i].Refresh();
		}
	}

	public Sprite GetCueSprite(string name)
	{
		for (int i = 0; i < cueSpriteList.Length; i++)
		{
			if (cueSpriteList[i].name.Equals(name))
			{
				return cueSpriteList[i];
			}
		}
		return null;
	}

	public void SwitchPanel(bool showCue)
	{
		if (_showCuePanel != showCue)
		{
			_showCuePanel = showCue;
			cuePanel.gameObject.SetActive(_showCuePanel);
			commonPanel.SetActive(!_showCuePanel);
			buyItemBtn.GetComponent<Image>().enabled = !_showCuePanel;
			switchCueBtn.GetComponent<Image>().enabled = _showCuePanel;
		}
	}
}
